﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using _MetalSlug.Form.ScreenSystem;
using _MetalSlug.Form.ScreenSystem.Message;
namespace _MetalSlug.Form.SelectCharScreen
{
    public class SelectCharScreen : MyScreen
    {
        List<CharacterDisplayer> Characters;
        int currentIndex;

        public int CurrentIndex
        {
            get { return currentIndex; }
            set 
            {
                Characters[CurrentIndex].CurrentState = CharacterDisplayer.STATE.DISABLE;
                Characters[value].CurrentState = CharacterDisplayer.STATE.ACTIVE;
                currentIndex = value;
            }
        }

        public SelectCharScreen()
        {
            LoadContent();
            InitialAttribute();
        }

        public override void LoadContent()
        {
            Characters = new List<CharacterDisplayer>();
            Background = GameUtil.LoadTexture("SelectChar/bground");
            List<Texture2D> lstTextures;
            MySprite SpriteDisplayChosen;
            String heroName, pathMainTexture, pathCoverTexture;
            Vector2 size = new Vector2(180,205);

            heroName = "Marco";
            pathMainTexture = "SelectChar/Marco";
            pathCoverTexture = "SelectChar/select";
            CharacterDisplayer marcoDisplayer = new CharacterDisplayer(heroName, pathMainTexture, pathCoverTexture,size);
            Characters.Add(marcoDisplayer);

            heroName = "Fio";
            pathMainTexture = "SelectChar/Fio";
            CharacterDisplayer fioDisplayer = new CharacterDisplayer(heroName, pathMainTexture, pathCoverTexture, size);
            Characters.Add(fioDisplayer);

            heroName = "Trevor";
            pathMainTexture = "SelectChar/Trevor";
            CharacterDisplayer trevorDisplayer = new CharacterDisplayer(heroName, pathMainTexture, pathCoverTexture, size);
            Characters.Add(trevorDisplayer);

            heroName = "Nadia";
            pathMainTexture = "SelectChar/Nadia";
            CharacterDisplayer nadiaDisplayer = new CharacterDisplayer(heroName, pathMainTexture, pathCoverTexture, size);
            Characters.Add(nadiaDisplayer);

            Vector2 Scale = new Vector2(GameUtil.ScreenSize.X/Background.Width, GameUtil.ScreenSize.Y/Background.Height);
            for (int i = 0; i < Characters.Count;i++ )
            {
                Vector2 positionOfDisplayChosen = new Vector2(i*64*Scale.X + 81, 71);
                String pathContent = String.Format("SelectChar/P1-{0}-",i.ToString("00"));
                lstTextures = GameUtil.LoadTextures(pathContent,3);
                SpriteDisplayChosen = new MySprite(lstTextures,positionOfDisplayChosen);
                Characters[i].ListDisplayChosen.Add(SpriteDisplayChosen);

                Vector2 newTopLeft = new Vector2(45+i*180,110);
                Characters[i].SetTopLeft(newTopLeft,Scale);
                Characters[i].SetSpeedCover(new Vector2(0,(i+1)*5)); 
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            foreach(CharacterDisplayer Character in Characters)
            {
                Character.Draw(spriteBatch);
            }

            base.Draw(spriteBatch);
            
            if (Characters[CurrentIndex].isCanChoose())
            {
                Characters[CurrentIndex].ListDisplayChosen[0].Draw(spriteBatch);
            }
                
        }

        public override void Update(GameTime gameTime)
        {
            if (Characters[CurrentIndex].isCanChoose() && Characters[CurrentIndex].CurrentState == CharacterDisplayer.STATE.DISABLE)
            {
                Characters[CurrentIndex].CurrentState = CharacterDisplayer.STATE.ACTIVE;
            }
            

            for(int i=0;i<Characters.Count;i++)
            {
                Characters[i].Update(gameTime);
            }

            HandleKeyboard();
            if(CheckForPlayGame())
            {
                this.Send(TYPE.LOADING, MyMessage.TYPE.SHOW,String.Format("{0} {1} {2}",Characters[CurrentIndex].Name, 0,0));
            }

        }

        public override void HandleKeyboard()
        {
            if(Characters[CurrentIndex].CurrentState== CharacterDisplayer.STATE.CHOSEN)
            {
                return;
            }

            if(UserInput.isKeyClicked(Keys.Right))
            {
                CurrentIndex = (CurrentIndex + 1) % Characters.Count;
            }

            if (UserInput.isKeyClicked(Keys.Left))
            {
                CurrentIndex = (CurrentIndex + Characters.Count - 1) % Characters.Count;
            }

            if (UserInput.isKeyClicked(Keys.Enter) && 
                Characters[CurrentIndex].CurrentState == CharacterDisplayer.STATE.ACTIVE &&
                Characters[CurrentIndex].isCanChoose())
            {
                Characters[CurrentIndex].CurrentState = CharacterDisplayer.STATE.CHOSEN;
                for (int i = 0; i < Characters.Count;i++ )
                {
                    if(i!= CurrentIndex)
                    {
                        Characters[i].CloseCover();
                    }
                }
            }

            if (UserInput.isKeyClicked(Keys.Escape))
            {
                this.Send(TYPE.MAINMENU, MyMessage.TYPE.SHOW, null);
            }
        }

        public override void InitialAttribute()
        {
            TypeOfWindow = TYPE.SELECTCHAR;
            if(Characters[CurrentIndex].CurrentState != CharacterDisplayer.STATE.DISABLE)
            {
                Characters[CurrentIndex].CurrentState = CharacterDisplayer.STATE.DISABLE;
            }

            foreach(CharacterDisplayer Character in Characters)
            {
                Character.OpenCover();
                Character.ResetPositionCover();
            }
            CurrentIndex = 0;
        }

        public Boolean CheckForPlayGame()
        {
            if (Characters[CurrentIndex].CurrentState == CharacterDisplayer.STATE.CHOSEN)
            {
                Boolean existItemNotClose = false;
                for (int i = 0; i < Characters.Count; i++)
                {
                    if (i != CurrentIndex && !Characters[i].isCanClose())
                    {
                        existItemNotClose = true;
                    }
                }

                if (!existItemNotClose)
                {
                    return true;
                }
            }

            return false;
        }
    }
}
